<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="医院：">
          <hospital-select v-model="formInline.hospitals"></hospital-select>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="formInline.enableStatus" clearable>
            <el-option label="启用" :value="1"></el-option>
            <el-option label="停用" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入关键词"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <auth-button uid="1060302" type="primary" size="small" @click.native="addAcount()">新增账号</auth-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="hospitalName" label="医院名称" width="400px">
          <template slot-scope="{row}">
            <tab-hospital :user="showFormatter(row)"></tab-hospital>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="姓名" show-overflow-tooltip />
        <el-table-column prop="account" label="登录手机" show-overflow-tooltip />
        <el-table-column prop="panTypeStr" label="类型" show-overflow-tooltip />
        <el-table-column prop="enableStatusStr" label="状态" show-overflow-tooltip />
        <el-table-column prop="createdDate" label="创建时间" show-overflow-tooltip />
        <el-table-column prop="principal" label="负责人" show-overflow-tooltip />
        <el-table-column prop="lastLoginTime" label="最后一次登录时间" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" width="200px" align="center">
          <template slot-scope="{row}">
            <auth-link uid="1060303" type="primary" class="el_link" :underline="false" :id="row.hospitalId"
              @click.native="addAcount(row.id)">编辑</auth-link>
            <auth-link uid="1060304" type="primary" class="el_link" :underline="false" :id="row.hospitalId"
              @click.native="updateUserInfo(row)">{{row.enableStatus == 1?'停用':'启用'}}</auth-link>
            <auth-link uid="1060305" type="primary" class="el_link" :underline="false" :id="row.hospitalId"
              @click.native="removeAcount(row)">删除</auth-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal"
          :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList"
          @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <add-acount v-model="showDialog" :id="currentId" @change="onChange" v-if="showDialog"></add-acount>

  </div>
</template>

<script>
import { showToast, showModal, deepClone } from "@/common/util";
import TabHospital from "@/components/hospital";
import { GET_HOSPITAL_ACCOUNTLIST, DELETEHOSPITALINFO } from "@/http/hospital";
import AddAcount from './addAcount.vue'
import {UPDATEUSERSTATUS,DELETEUSERINFO} from '@/http/system'
export default {
  name: "BookPage",
  props: {
    status: {
      type: [String, Number],
      default: "",
    },
  },
  components: {
    TabHospital,
    AddAcount
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      showDialog: false,
      currentId: ''
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = { isAppointmentOpened: 1 };
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GET_HOSPITAL_ACCOUNTLIST(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 新增账号
    addAcount(id) {
      this.showDialog = true
      if (id) {
        this.currentId = id
      }
    },

    // 格式化医院数据
    showFormatter(row) {
      if (!row) {
        return {};
      }
      let o = deepClone(row)
      o.hospitalName = row.hospitalName;
      o.hospitalAddress = row.hospitalAddress;
      o.city = row.hospitalCity
      o.id = row.hospitalId
      return o;
    },

    // 删除账号
    removeAcount(row) {
      let _this = this;
      showModal({
        message: `确定删除该账号吗？`,
        showCancelButton: true,
        success: function () {
          DELETEUSERINFO({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 监听
    onChange(e) {
      this.currentId = ''
      if (e) {
        this.getList()
      }

    },

    // 启用停用
    updateUserInfo(row) {
      let _this = this;
      // todo
      let _type = {
        1: "禁用",
        2: "启用",
      };
      showModal({
        message: `确定${_type[row.enableStatus]}该账号吗？`,
        showCancelButton: true,
        type: "warning",
        success: function () {
          let oParams = {};
          oParams.id = row.id;
          UPDATEUSERSTATUS(oParams).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "操作成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },
    
    // 搜索
    onSearch(){
      this.pageNumber = 1;
      this.getList()
    }

  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
</style>
